package mongodb

import (
	"fmt"
	"gitee.com/dreamwood/ez"
	"os/exec"
)

func CreateController(c *Creator) {
	content := twigger.Render("/mongodb/controller.twig", c)
	createControllerFile(c, []byte(content))
}

func createControllerFile(c *Creator, content []byte) {
	path := fmt.Sprintf(
		"%s/app/%s/auto/v0/api/%sApi.go",
		root, c.Table.Path, c.Table.Name)
	ez.CreateFile(path, content)
	wg.Add(1)
	go func() {
		e := exec.Command("go", "fmt", path).Run()
		if e != nil {
			println("go fmt 5", e.Error())
		}
		wg.Done()
	}()
}

func CreateEmptyController(c *Creator) {
	content := twigger.Render("/mongodb/controller_admin.twig", c)
	createEmptyControllerFile(c, []byte(content))
}

func createEmptyControllerFile(c *Creator, content []byte) {
	path := fmt.Sprintf(
		"%s/app/%s/controller/admin/%sApi.go",
		root, c.Table.Path, c.Table.Name)
	if !ez.FileExist(path) || true {
		ez.CreateFile(path, content)
		wg.Add(1)
		go func() {
			e := exec.Command("go", "fmt", path).Run()
			if e != nil {
				println("go fmt 5", e.Error())
			}
			wg.Done()
		}()
	}
}
